﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFDynamicProxyLibrary.DynamicProxy;
using System.Threading;
using Broker.Model;
using NHibernate;
using BrokerBase.Lib.Client;
using Broker.Comm;
using Broker.DataUpdateWcf;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Broker.Update
{
    

    public class ServiceFactory
    {
        public const string DATAUPDATE_SERVICE_NAME = "DataUpdateService";
        public const string LICENSE_SERVICE_NAME = "licenseService";

        private const int MAX_LENGTH = 256 * 1024 * 1024;//256M的最大数据

        private static string hostAddress;

        private static object syncHostAddress = new object();


        private ServiceFactory()
        {

        }

        private static string HostAddress
        {
            get
            {
                lock (syncHostAddress)
                {
                    if (string.IsNullOrEmpty(hostAddress))
                    {
                        hostAddress = AppGlobal.ClientCfg.ServerUrl1;
                    }
                    return hostAddress;
                }
            }
        }

        public static ServiceFactory GetInstance()
        {
            return new ServiceFactory();
        }


        public T CreateService<T>(string name) 
        {
            WSHttpBinding bind = CreateHttpBinding();

            EndpointAddress epa = CreateEndpointAddress(name);

            ChannelFactory<T> cf = new ChannelFactory<T>(bind,epa);

            cf.Open();
            T s= cf.CreateChannel();

            return s;
        }

        public DataUpdateServiceClient CreateUpdateService()
        {

            WSHttpBinding bind = CreateHttpBinding();

            EndpointAddress epa = CreateEndpointAddress(DATAUPDATE_SERVICE_NAME);

            DataUpdateServiceClient dataService = new DataUpdateServiceClient(bind,epa);

            return dataService;
        
        }


        private WSHttpBinding CreateHttpBinding()
        {
            WSHttpBinding bind = new WSHttpBinding();

            bind.ReaderQuotas.MaxArrayLength = MAX_LENGTH;//设置最大可接受的数组
            bind.ReaderQuotas.MaxBytesPerRead = MAX_LENGTH;
            bind.ReaderQuotas.MaxStringContentLength = MAX_LENGTH;
            bind.ReaderQuotas.MaxDepth = 64;

            bind.MaxBufferPoolSize = MAX_LENGTH;
            bind.MaxReceivedMessageSize = MAX_LENGTH;

            bind.SendTimeout = new TimeSpan(0, 5, 0);
            bind.ReceiveTimeout = new TimeSpan(0, 5, 0);

            bind.Security.Message.EstablishSecurityContext = false;
            bind.Security.Mode = SecurityMode.None;

            bind.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
            bind.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;

            return bind;
        }

        private EndpointAddress CreateEndpointAddress(string serviceName)
        {
            Uri uri = new Uri(HostAddress.TrimEnd("/".ToCharArray()) + "/" + serviceName );

            EndpointIdentity ei = EndpointIdentity.CreateDnsIdentity("localhost");

            EndpointAddress endpointAddress = new EndpointAddress(uri,ei);

            


            return endpointAddress;
        }
    }
}
